<html>
<head><meta charset="utf-8"><title>Adding a target with a linker script · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html">Adding a target with a linker script</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196952090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196952090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196952090">(May 08 2020 at 21:21)</a>:</h4>
<p>Hello! I'm trying to add a new target to rustc, however it requires a specific linker script to be used with lld. I already have a target JSON working with a local linker script and <code>pre_link_args</code>, however I would like to open a PR to merge it upstream as a built in target, along with this mandatory linker script. Is this possible at the moment?</p>



<a name="196952579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196952579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196952579">(May 08 2020 at 21:26)</a>:</h4>
<p>What does the linker script do?</p>



<a name="196952694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196952694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196952694">(May 08 2020 at 21:28)</a>:</h4>
<p>It has specific sections that look like .rodata.xxx which* can’t be merged into .rodata as the platform needs to read them separately</p>



<a name="196952742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196952742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196952742">(May 08 2020 at 21:29)</a>:</h4>
<p>So the script just ensures they are output correctly</p>



<a name="196953394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196953394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196953394">(May 08 2020 at 21:36)</a>:</h4>
<p>Interesting. Is there a rule for what goes in each <code>.rodata.xxx</code> section?</p>



<a name="196953415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196953415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196953415">(May 08 2020 at 21:37)</a>:</h4>
<p>And does the program's own read-only data go in <code>.rodata</code> or in one of those sections?</p>



<a name="196953544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196953544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196953544">(May 08 2020 at 21:38)</a>:</h4>
<p>The regular read only data goes into .rodata, but there is special meta information about the output ELF that needs to be stored in these .rodata.xxx sections (such as a pointer to the entry function or name of your module)</p>



<a name="196953602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196953602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196953602">(May 08 2020 at 21:39)</a>:</h4>
<p>The platform also uses these sections for linking, so you declare your imports in one of these libraries. Similarly there is a special .text section for stubs though IIRC that gets passed through correctly with the default LLD script</p>



<a name="196953980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196953980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196953980">(May 08 2020 at 21:43)</a>:</h4>
<p>Makes sense.</p>



<a name="196954041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954041">(May 08 2020 at 21:44)</a>:</h4>
<p>Is it possible to teach LLVM about this as part of the target's linker defaults? It seems like you'd need this for C and C++, too.</p>



<a name="196954553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954553">(May 08 2020 at 21:50)</a>:</h4>
<p>It is probably possible, but I'm not sure there is any demand for that. For context this is a homebrew game system target from 2005 that has a fairly decent unofficial GCC-based toolchain (Sony PSP). There doesn't seem to be any demand for enabling LLVM C/C++ support. If it's the only way, I'll end up giving that a shot, but keeping it only in rust with a single linker script would be much easier</p>



<a name="196954590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954590">(May 08 2020 at 21:50)</a>:</h4>
<p>Ah, I see.</p>



<a name="196954628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954628">(May 08 2020 at 21:51)</a>:</h4>
<p>It's certainly not the <em>only</em> way; it's more that it's likely the <em>easiest</em> way, from a technical perspective. Of course, policies may vary.</p>



<a name="196954656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954656">(May 08 2020 at 21:51)</a>:</h4>
<p>Has someone tried getting the target and linker script into LLVM and gotten rejected?</p>



<a name="196954711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954711">(May 08 2020 at 21:52)</a>:</h4>
<p>I do think, in general, Rust has much more inclination to add tier-3 targets if at least some folks care about those targets.</p>



<a name="196954760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954760">(May 08 2020 at 21:52)</a>:</h4>
<p>I feel like LLVM ought to have the same kind of "tiering" policy, since there is interest in uncommon targets, and developers just don't want to guarantee that they'll remain working.</p>



<a name="196954769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196954769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196954769">(May 08 2020 at 21:53)</a>:</h4>
<p>I am not aware that anybody has attempted to do this with LLVM. There was at one point a CPU target, though it was discovered to be basically identical to MIPS2 later down the line and they removed it (cpu = "mips2" works perfectly with rust atm).</p>



<a name="196955284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196955284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196955284">(May 08 2020 at 21:58)</a>:</h4>
<p>For some context I've found the targets here: <a href="https://github.com/rust-lang/rust/tree/master/src/librustc_target/spec" title="https://github.com/rust-lang/rust/tree/master/src/librustc_target/spec">https://github.com/rust-lang/rust/tree/master/src/librustc_target/spec</a><br>
Though none of them use a custom linker script, so I'm not sure how to go about adding one.</p>



<a name="196955289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196955289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196955289">(May 08 2020 at 21:59)</a>:</h4>
<p>If it's not <em>too</em> much trouble, and the target really just needs a linker script and is otherwise identical to some existing target, you might consider submitting the patch to LLVM. If they decline to support it, then I imagine it could be added as a tier-3 target in Rust anyway.</p>



<a name="196955348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196955348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196955348">(May 08 2020 at 21:59)</a>:</h4>
<p>(As for how to add a target-specific linker script to a Rust target, I don't know, but it absolutely <em>ought</em> to be possible.)</p>



<a name="196955889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196955889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196955889">(May 08 2020 at 22:05)</a>:</h4>
<p>The target is basically identical to a bare metal <code>mipsel</code>target, but I've never submitted a patch to LLVM, so I'm not sure where I would even begin. Do you know anybody who would know how to add such a linker script to a rust built in target? Or perhaps where I could ask for help? If the LLVM patch method is strongly preferred, I'll have to go down that route.</p>
<p>As it stands, I think it's much more likely I'd succeed in adding it to rustc than LLVM, if that is acceptable. (The homebrew community does not seem to care about LLVM support currently, so it's very low priority)</p>



<a name="196955961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196955961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196955961">(May 08 2020 at 22:06)</a>:</h4>
<p>You're likely in the right place, and you might also try filing an issue and asking for help, with the linker script attached.</p>



<a name="196955982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196955982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196955982">(May 08 2020 at 22:06)</a>:</h4>
<p>To be clear, there's nothing <em>wrong</em> with just having support in Rust; it just means that that target may have issues working with C and C++.</p>



<a name="196956321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20a%20target%20with%20a%20linker%20script/near/196956321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Marko Mijalkovic <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20a.20target.20with.20a.20linker.20script.html#196956321">(May 08 2020 at 22:11)</a>:</h4>
<p>Ok. Thank you for the tips! I'll watch this thread for a bit to see if anybody can help. Otherwise I'll go ahead and file an issue.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>